use "\meta informalityWP.dta" , clear


*Study name
gen str30 studyname="Baker & Velasco-Guachalla 2018" if source==2
replace studyname="Carnes & Mares 2016" if source==4
replace studyname="Altamirano Hernández 2015" if source==7
replace studyname="Menéndez González 2021" if source==11
replace studyname="Altamirano et al 2022" if source==26
replace studyname="BEPS 2018*" if source==28
replace studyname="IBOPE/SECOM-PR 2014*" if source==29

*Data Management. Take average of models that are almost the same
gen dv_class=2 if dvabbrev=="Redistribution"
replace dv_class=1 if dvabbrev=="Social policy"

keep if dv_class==1 | dv_class==2
keep if region=="Latin"

gen proper_baseline=1 if properbaselineformalsector=="Yes"
replace proper_baseline=0 if properbaselineformalsector=="No"

collapse (firstnm) country table unitofanalysis codingofdvbinaryordinalcontinuou dvsupportfornoncontribprogramred dvabbrev definitionofinformalityiv codingofinformalityivbinaryordin region label countries journalpublication journalname journalimpactfactor yearofdatacollection studyname agree nonbinary tradeoff ///
(mean) n numberofmodelestimatorsk degreesoffreedomnk coeffoninformal seoninformality coeffoninformal2 seoninformality2 coeffonwealth seonwealth coeffonincome seonincome coeffoneducation seoneducation tstatoninformality partialcorrelationoninformalityr tstatonwealth partialcorrelationonwealthr tstatonincome partialcorrelationonincomer tstatoneducation partialcorrelationoneducationr effectofinformalinsd effectofinformalonprob  ///
(max) yearpublished dv_class proper_baseline, by(source modelsetup )

*Precision
gen se_informal_partial=((1-partialcorrelationoninformalityr^2)/degreesoffreedomnk)^.5 
gen precision_informal_coeff=1/se_informal_partial
*Weight
gen weightFE=1/(se_informal_partial^2)

*Calculating Average Effects
	*Social policy
	*1. unweighted mean
mean partialcorrelationoninformalityr if dvabbrev=="Social policy" 

	*2. weighted mean by Ns, p. 46 of Stanley
mean partialcorrelationoninformalityr if dvabbrev=="Social policy" [aweight=n]

	*3. FEE, weighted mean by optimal weights, p. 46 of Stanley
*mean partialcorrelationoninformalityr if dvabbrev=="Social policy" [aweight=weightFE]
metaan partialcorrelationoninformalityr se_informal_partial if dvabbrev=="Social policy", fe

	*4. REE, weighted mean, p. 46 of Stanley. 
	*THIS IS THE ONE THAT'S REPORTED IN THE PAPER BECAUSE ASSUMES DIFFERENT UNDERLYING POPULATIONS, UNLIKE FEE
metaan partialcorrelationoninformalityr se_informal_partial  if dvabbrev=="Social policy" , bdl label(studyname) forest
local REE=r(eff)
display `REE'
local REEse=r(effvar)^.5

*-----------------------------------------------------------------
	*FIGURE 1
*READ OUT TO NEW FILE TO GENERATE META-ANALYSIS FIGURE
preserve
local studiesplus1 19 //change this each time you add estimates
keep if dvabbrev=="Social policy"
keep studyname partialcorrelationoninformalityr se_informal_partial country label
gsort -partialcorrelationoninformalityr
expand 2 if _n==18 //change this to the number of estimates
replace partialcorrelationoninformalityr = `REE' in `studiesplus1'
replace studyname = "Weighted average" in `studiesplus1'
replace se_informal_partial = `REEse' in `studiesplus1'
replace country = "" in `studiesplus1'
replace label = "" in `studiesplus1'
gen n=_n
recode n `studiesplus1'=0
gen lower=partialcorrelationoninformalityr -1.96*se_informal_partial 
gen upper=partialcorrelationoninformalityr +1.96*se_informal_partial 
gen placement=.24
*Also super important to update the axis labels since order of studies changes
twoway (rcap lower upper n if n~=0, horizontal color(gs10)) (scatter n partialcorrelationoninformalityr if n~=0, mcolor(gs10)) (scatter n placement if n~=0, msym(i) mlab(label) mlabcolor(black) mlabsize(small)) ///
(rcap lower upper n if n==0, horizontal color(black)) (scatter n partialcorrelationoninformalityr if n==0, mcolor(black)), ///
ylabel(0 "Weighted average" 1 "Menéndez González 2021" 2 "IBOPE/SECOM-PR 2014*" 3 "Altamirano Hernández 2015" 4 "Altamirano Hernández 2015" 5 "Altamirano Hernández 2015" 6 "Baker & Velasco-Guachalla 2018" 7 "Baker & Velasco-Guachalla 2018" 8 "Baker & Velasco-Guachalla 2018" ///
9 "IBOPE/SECOM-PR 2014*" 10 "Baker & Velasco-Guachalla 2018" 11 "Baker & Velasco-Guachalla 2018" 12 "Carnes & Mares 2016" 13 "Carnes & Mares 2016" 14 "Baker & Velasco-Guachalla 2018" 15 "Altamirano Hernández 2015" 16 "BEPS 2018*" 17 "BEPS 2018*" 18 "Altamirano et al 2022", angle(horizontal) nogrid labsize(small)) ///
legend(off) xtitle("{bf:Partial Correlation between Informality and DV}") ytitle("{bf:Source}") xline(0, lcolor(black) lwidth(vthin)) yline(.5, lcolor(black)) ///
graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black))
restore
*-----------------------------------------------------------------

gen varoninformality=se_informal_partial^2
recode nonbinary (1=0) (0=1), gen(binary)
gen interact=agree*binary
metareg partialcorrelationoninformalityr agree binary interact if dvabbrev=="Social policy", wsse(se_informal_partial)

	*reported in text
metaan partialcorrelationoninformalityr se_informal_partial if dvabbrev=="Social policy" & agree==1, bdl label(studyname) forest
metaan partialcorrelationoninformalityr se_informal_partial if dvabbrev=="Social policy" & agree==0, bdl label(studyname) forest

	*Change in S.D. Can't trust standard error here but just getting mean 
impute effectofinformalinsd partialcorrelationoninformalityr if dvabbrev=="Social policy" , gen(effectofinformalinsd_imputed)
	*4. REE, weighted mean, p. 46 of Stanley. I use country rather than study as the source of heterogeneity
metaan effectofinformalinsd_imputed se_informal_partial  if dvabbrev=="Social policy" , bdl label(country)

	*Change in prob
	*4. REE, weighted mean, p. 46 of Stanley. 
metaan effectofinformalonprob  se_informal_partial  if dvabbrev=="Social policy" , bdl label(country)

	*Quick look at education
gen se_educ_partial=((1-partialcorrelationoneducationr ^2)/degreesoffreedomnk)^.5 
metaan partialcorrelationoneducationr se_educ_partial if dvabbrev=="Social policy", bdl label(studyname) forest


*Funnel plot from earlier version of paper
gen statsig=1 if tstatoninformality>1.96 |  tstatoninformality<-1.96  
recode statsig .=0
replace statsig=. if tstatoninformality==.

tab statsig if dvabbrev=="Social policy"
tab statsig if dvabbrev=="Redistribution"

gen labelsig= "{bf:" + label + "}" 

gen mlabpos=9
replace mlabpos=6 if source==7 & modelsetup==7
replace mlabpos=12 if source==7 & modelsetup==8
replace mlabpos=3 if source==7 & modelsetup==10
replace mlabpos=3 if source==2 & modelsetup==9
replace mlabpos=3 if source==2 & modelsetup==9
replace mlabpos=3 if source==24 & modelsetup==3
replace mlabpos=12 if source==2 & modelsetup==11
replace mlabpos=3 if source==2 & modelsetup==12
replace mlabpos=6 if source==4 & modelsetup==6
replace mlabpos=7 if source==2 & modelsetup==7
replace mlabpos=3 if source==2 & modelsetup==10


twoway (scatter precision_informal_coeff partialcorrelationoninformalityr  if dvabbrev=="Social policy" & statsig==1, msym(o) mcolor(black) mlab(labelsig) mlabvpos(mlabpos) mlabsize(medsmall) mlabcolor(black) mlabgap(0)) ///
(scatter precision_informal_coeff partialcorrelationoninformalityr  if dvabbrev=="Social policy" & statsig==0, msym(o) mcolor(black) mlab(label) mlabvpos(mlabpos) mlabsize(small) mlabcolor(black) mlabgap(0)), /// 
graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) xline(0, lcolor(black))  legend(off) xscale(range(-.062 .16)) xtitle("{bf:Partial Correlation between Informality and DV}") ytitle("{bf:Precision of Estimates}") ///
xline( `REE'  , lwidth(16.5) lc(gs15)) xline(`REE' , lc(black) lpattern(dash))
